package com.evoupsight.monitorpass.alarm.services;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @author evoup
 */
@Component
public class ScanEventTask {
    private static final Logger LOG = LoggerFactory.getLogger(ScanEventTask.class);

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("HH:mm:ss");

//    @Value("${zk.servers}")
//    String zkServers;

    private final ScanService scanService;

    @Autowired
    public ScanEventTask(ScanService scanService) {
        this.scanService = scanService;
    }

    @Scheduled(fixedDelay = 10000)
    public void doAllJobs() {
        int baseSleepTimeMills = 1000;
        int maxRetries = 3;
        LOG.info("alarm ready to work--------------------------------");
        scanService.doAllJobs();
        LOG.info("alarm job done!");

//        RetryPolicy retryPolicy = new ExponentialBackoffRetry(baseSleepTimeMills, maxRetries);
//        try (CuratorFramework cfClient = CuratorFrameworkFactory.newClient(zkServers, retryPolicy)) {
//            cfClient.start();
//            InterProcessLock lock = new InterProcessMutex(cfClient, LOCK_PATH);
//            if (lock.acquire(DEFAULT_WAIT_TIME_SECONDS, TimeUnit.SECONDS)) {
//                try {
//                    LOG.info("alarm ready to work");
//                    scanService.doAllJobs();
//                    LOG.info("alarm job done");
//                } finally {
//                    lock.release();
//                }
//            } else {
//                LOG.error("{} timed out after {} seconds waiting to acquire lock on {}",
//                        MY_NAME, DEFAULT_WAIT_TIME_SECONDS, LOCK_PATH);
//            }
//        } catch (Exception e) {
//            LOG.error(e.getMessage(), e);
//        }

        LOG.info("The time is now {}", DATE_FORMAT.format(new Date()));
    }
}
